<?php

namespace app\home\controller;

use think\Db;
use clt\Lunar;
use think\facade\Env;

class Index extends Common
{
    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $banner      = db('setting')->where(['key'=>'banner'])->select();
        $this->assign('banner',$banner);
        return $this->fetch();
    }

    /**
     * designer
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/31 0031 22:02
     */
    public function designer()
    {
        $year=input('year')?:date('Y');
        $user             = db('users');
        $data=$user->where("year",'=',$year)->select();

        $source=db('user_year')->order('year')->select();
        $source=array_column($source,null,'year');
        $year_list=array_column($source,'year');
        $desc=$source[$year]['description'];
       
        $this->assign('year_list',$year_list);
        $this->assign('desc',$desc);
        $this->assign('year',substr($year,2));
        $this->assign('data',$data);


        $model             = db('setting');
        $detail=$model->where(['key'=>'user_detail'])->find();
        
        $this->assign('detail', $detail);

        return $this->fetch();
    }

    /**
     * keynote book
     * @return mixed
     * @throws \think\exception\DbException
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/31 0031 22:02
     */
    public function keynote()
    {
        $page=input('page')?:1;
        $result     = Db::name('keynote_book')->order('createtime')
            ->paginate(['list_rows' => 20, 'page' => $page]);
        $this->assign('result',$result);

        $result  =$result->toArray();
        $main_id=array_column($result['data'],'id');

        $source     =$main_id? Db::name('keynote_book_file')
            ->whereIn('main_id',$main_id)
            ->select():[];
        $file=[];
        foreach ($source as $item) {
            $file[$item['main_id']]['imglist'][]=$item['path'];
        }
        foreach ($result['data'] as $item) {
            $file[$item['id']]['description']=$item['description'];
        }
        $data=array_chunk($result['data'],4);
        $this->assign('file',json_encode($file,JSON_UNESCAPED_SLASHES));
        $this->assign('data',$data);

        return $this->fetch();
    }

    /**
     * thinking
     * @return mixed
     * @throws \think\exception\DbException
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/31 0031 22:02
     */
    public function thinking()
    {
        $page=input('page')?:1;
        $result     = Db::name('design')->order('createtime')
            ->paginate(['list_rows' =>20, 'page' => $page]);
        $this->assign('result',$result);
        $result=$result->toArray();
        $main_id=array_column($result['data'],'id');
        $source     =$main_id? Db::name('design_file')
            ->whereIn('main_id',$main_id)
            ->field('path,main_id')
            ->select():[];

        $file=[];
        foreach ($source as $item) {
            $file[$item['main_id']]['imglist'][]=$item['path'];
        }
        foreach ($result['data'] as $item) {
            $file[$item['id']]['description']=$item['description'];
        }
        $this->assign('file',json_encode($file,JSON_UNESCAPED_SLASHES));

        $data=array_chunk($result['data'],5);
        $this->assign('data',$data);
     

        return $this->fetch();
    }

    /**
     * contact
     * @return mixed
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/31 0031 22:02
     */
    public function contact()
    {
        return $this->fetch();
    }

    public function detail()
    {
        $id=input('id');
        return db('users')->where("id",'=',$id)->find();
    }

}
